package com.xiaoyu.client.ui.dialog;

import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.google.gson.Gson;
import com.xiaoyu.client.R;
import com.xiaoyu.client.model.seek.SeekTypeBean;
import com.xiaoyu.client.model.seek.SeekTypeBean.SeekTypeParam;
import com.xiaoyu.client.network.NetworkManager;
import com.xiaoyu.commonlib.network.CommonHttpResponser;
import com.xiaoyu.commonlib.ui.widget.LabelsView;
import com.xiaoyu.commonlib.utils.UIUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by jk on 2018/9/29.
 */
public class HelpFilterPopupWindow extends PopupWindow implements View.OnClickListener {
    private View mView;
    private RadioGroup statusGroup;
    private LabelsView labelsView;
    private Context mContext;
    private TextView cancelBtn;
    private TextView confirmlBtn;

    private List<SeekTypeParam> mTypeList = new ArrayList<>();
    private Map<String,SeekTypeParam> mSelectList = new HashMap<>();
    private int status = -1;//0:未解决,1:已解决，’-1’:全部

    public HelpFilterPopupWindow(Context context) {
        super(context);
        mContext = context;
        initView(context);
        getTypeInfo();
    }

    private void initView(Context context) {
        mView = View.inflate(context, R.layout.popup_help_filter, null);
        statusGroup = mView.findViewById(R.id.popup_help_filter_status_layout);
        labelsView = mView.findViewById(R.id.popup_help_filter_type_list);
        cancelBtn = mView.findViewById(R.id.popup_help_filter_cancel_btn);
        confirmlBtn = mView.findViewById(R.id.popup_help_filter_confirm_btn);


        labelsView.setOnLabelSelectChangeListener(selectChangeListener);
        cancelBtn.setOnClickListener(this);
        confirmlBtn.setOnClickListener(this);
        statusGroup.setOnCheckedChangeListener(onCheckedChangeListener);

        //华为手机软件盘
        this.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

        this.setContentView(mView);
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        this.setHeight(UIUtils.dip2px(300));
        this.setOutsideTouchable(false);
        this.setFocusable(false);
        this.setBackgroundDrawable(new ColorDrawable(0));
        this.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
        this.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
    }


    @Override
    public void onClick(View v) {
        if (v == cancelBtn) {
            status = -1;
            mSelectList.clear();
            dismiss();
        } else if (v == confirmlBtn) {
            if (mListener != null){
                mListener.onSeekType(status,mSelectList);
            }
            dismiss();
        }
    }

    private LabelsView.OnLabelSelectChangeListener selectChangeListener = new LabelsView.OnLabelSelectChangeListener() {
        @Override
        public void onLabelSelectChange(View label, String labelText, boolean isSelect, int position) {
            if (isSelect){
                if (!mSelectList.containsKey(mTypeList.get(position).getSeephelpid())){
                    mSelectList.put(mTypeList.get(position).getSeephelpid(),mTypeList.get(position));
                }
            }else {
                if (mSelectList.containsKey(mTypeList.get(position).getSeephelpid())){
                    mSelectList.remove(mTypeList.get(position).getSeephelpid());
                }
            }
        }
    };

    private RadioGroup.OnCheckedChangeListener onCheckedChangeListener = new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            switch (checkedId) {
                case R.id.popup_help_filter_status_all://全部
                    status = -1;
                    break;
                case R.id.popup_help_filter_status_solved://已解决
                    status = 1;
                    break;
                case R.id.popup_help_filter_status_unsolved://未解决
                    status = 0;
                    break;
            }
        }
    };


    /**
     * 获取求助类别信息
     */
    private void getTypeInfo(){
        mTypeList.clear();
        NetworkManager.getSeekType(new CommonHttpResponser() {
            @Override
            public void onSuccess(String result) {
                parseTypeInfoSuc(result);
            }

            @Override
            public void onFailed(String errorCode, String errorDiscription) {

            }
        });
    }

    private void parseTypeInfoSuc(String result) {
        SeekTypeBean bean = new Gson().fromJson(result,SeekTypeBean.class);
        mTypeList.addAll(bean.getType());
        ArrayList<String> list = new ArrayList<>();
        for (int i = 0;i<mTypeList.size();i++){
            list.add(mTypeList.get(i).getSeephelpname());
        }
        labelsView.setLabels(list);
    }


    @Override
    public void showAsDropDown(View anchor) {
        if (Build.VERSION.SDK_INT >= 24) {
            Rect rect = new Rect();
            anchor.getGlobalVisibleRect(rect);
            int h = anchor.getResources().getDisplayMetrics().heightPixels - rect.bottom;
            setHeight(h);
        }
        super.showAsDropDown(anchor);
    }


    private OnSeekTypeListener mListener;
    public interface OnSeekTypeListener{
        void onSeekType(int status,Map<String,SeekTypeParam> typeList);
    }

    public void setOnSeekTypeListener(OnSeekTypeListener listener){
        this.mListener = listener;
    }
}
